package com.hy.dao;

import com.hy.bean.Comment;
import com.hy.bean.Discuss;
import com.hy.utils.DBManager;
import com.hy.utils.DateUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class CommentDao {
    private QueryRunner qr = new QueryRunner();

    //添加评论喜欢
    public boolean insertDiscuss(Discuss discuss) throws Exception {
        Connection conn = DBManager.getConn();

        String sql = "INSERT INTO discuss VALUES (null, ?, ?, ?, ?, ?, '0');";
        int count = qr.update(conn, sql,discuss.getUserId(),discuss.getCommentId(),DateUtil.datetime(),discuss.getUpdateTime(),discuss.getCreateId());
        DbUtils.close(conn);

        return count > 0 ? true : false;
    }
    //查询喜欢数量
    public Long selectDiscussCount(Integer commentId) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select count(*) from discuss where comment_id = ? and tombstone='0'";
        Long query = qr.query(conn, sql, new ScalarHandler<Long>(),commentId);
        DbUtils.close(conn);
        return query;
    }
    //查询自己是否点赞
    public Discuss selectDiscuss(Integer userId,Integer commentId) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select * from discuss where user_id=? and comment_id=? ";
        Discuss query = qr.query(conn, sql, new BeanHandler<>(Discuss.class),userId,commentId);
        DbUtils.close(conn);
        return query;
    }
    public boolean updateTombstone(Integer id,String tombstone) throws Exception {
        Connection conn = DBManager.getConn();

        String sql = " UPDATE discuss SET tombstone=?,update_time=?  WHERE comment_id = ?";
        int count = qr.update(conn, sql, tombstone,DateUtil.datetime(),id);

        DbUtils.close(conn);

        return count > 0 ? true : false;
    }

}
